import 'package:flutter/material.dart';
import 'package:flutter_learn/learn/component/center_learn.dart';
import 'package:flutter_learn/learn/component/container_learn.dart';
import 'package:flutter_learn/learn/dialog/common_dialog_learn.dart';
import 'package:flutter_learn/learn/inherit/ConfigWidgetPage.dart';
import 'package:flutter_learn/learn/refresh/easyfresh_learn_page.dart';
import 'package:flutter_learn/learn/layout/align/align_layout_learn.dart';
import 'package:flutter_learn/learn/layout/flex/column_layout_learn.dart';
import 'package:flutter_learn/learn/layout/flex/row_layout_learn.dart';
import 'package:flutter_learn/learn/layout/stack/index_stack_layout_learn.dart';
import 'package:flutter_learn/learn/layout/stack/stack_layout_learn.dart';
import 'package:flutter_learn/learn/refresh/refresh_indicator_learn.dart';
import 'package:flutter_learn/learn/route/route_learn.dart';
import 'package:flutter_learn/learn/vido_player/vido_local.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'getx/number_counter.dart';

///flutter 学习工具
class LearnFlutter extends StatefulWidget {
  const LearnFlutter({Key? key}) : super(key: key);

  @override
  _LearnFlutterState createState() => _LearnFlutterState();
}

class _LearnFlutterState extends State<LearnFlutter> {
  DateTime? _lastPressedAt;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();

    WidgetsBinding.instance!.addPostFrameCallback((timeStamp) {
      print("初始化完成");
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        backgroundColor: Colors.grey,
        appBar: AppBar(title: Text("flutter learn")),
        body: WillPopScope(
            onWillPop: () async {
              if (_lastPressedAt == null || DateTime.now().difference(_lastPressedAt!) > Duration(seconds: 1)) {
                //两次点击间隔超过1秒则重新计时
                Fluttertoast.showToast(msg: "再按一次就会退出小笨猪");
                _lastPressedAt = DateTime.now();
                return false;
              }
              return true;
            },
            child: SingleChildScrollView(
              child: Container(
                padding: EdgeInsets.all(10.h),
                color: Colors.white,
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Row(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: [
                        //默认无线宽度不加会报错
                        Expanded(
                            child: Text(
                          "～～这是一个flutter学习框架;专门为小笨猪学flutter整理～～",
                          style: TextStyle(fontSize: 21.sp),
                          maxLines: 2,
                        ))
                      ],
                    ),
                    SizedBox(
                      height: 10.h,
                    ),
                    Text('布局学习'),
                    Divider(),
                    Wrap(
                      children: [
                        button("Stack布局", StackLayout()),
                        SizedBox(
                          width: 10.w,
                        ),
                        button("IndexStack布局", IndexStackLayout()),
                        button("Row布局", RowLayout()),
                        button("Column布局", ColumnLayout()),
                        button("Align布局", AlignLayout()),
                      ],
                    ),
                    Divider(),
                    Text('组件学习'),
                    Divider(),
                    Wrap(
                      children: [
                        button("Container", ContainerLearn()),
                        button("Center", CenterLearn()),
                      ],
                    ),
                    Divider(),
                    Text('弹框学习'),
                    Divider(),
                    Wrap(
                      children: [
                        button("commonDialog", CommonDialog()),
                      ],
                    ),
                    Divider(),
                    Text('路由学习'),
                    Divider(),
                    Wrap(
                      children: [
                        button("navigator", RouteHome()),
                      ],
                    ),
                    Divider(),
                    Text('状态管理'),
                    Divider(),
                    Wrap(
                      children: [
                        button("getX", NumberCounter()),
                      ],
                    ),
                    Text('refresh学习'),
                    Divider(),
                    Wrap(
                      children: [
                        button("easyFresh", EasyFreshPage()),
                        button("refreshIndicator", RefreshLearn()),
                      ],
                    ),
                    Divider(),
                    Text('audio/video'),
                    Divider(),
                    Wrap(
                      children: [
                        button("video", PlayerVideoAndPopPage()),
                      ],
                    ),
                    Text('inheritWidget'),
                    Divider(),
                    Wrap(
                      children: [
                        button("inheritWidget", ConfigWidgetPage()),
                      ],
                    ),
                  ],
                ),
              ),
            )));
  }

  _forward(Widget page) {
    Navigator.of(context).push(MaterialPageRoute(builder: (cxt) => page));
  }

  ///按钮封装
  Widget button(String name, Widget widget) {
    return OutlinedButton(
        onPressed: () {
          _forward(widget);
        },
        child: Text(name));
  }
}
